package org.numb.concurrency.chapter03;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
        // 并发开启5个线程处理业务， 等到业务处理完后，再处理结果
        for (int i = 0; i < 5; i++) {
            final int num = i;
            Thread thread = new Thread(() -> {
                System.out.println("线程" + num + "并发处理业务");
                try {
                    // 等待其余并发线程处理完成
                    cyclicBarrier.await();
                    System.out.println("线程" + num + "处理本线程的结果");
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }
    }
}
